package com.xuan.core.configuration.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.Cache;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.interceptor.CacheErrorHandler;
import org.springframework.cache.interceptor.SimpleCacheErrorHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * 配置类：Redis
 */
@Configuration
//@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {

    /**
     * 序列化
     */
    @Bean
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {

        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();

        redisTemplate.setConnectionFactory(redisConnectionFactory);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer(Object.class));

        return redisTemplate;

    }

    /**
     * 缓存异常
     */
    @Override
    public CacheErrorHandler errorHandler() {
        return new LoggingCacheErrorHandler();
    }

//--------------------------------------------------------------------------------------------------------------------------------

    static class LoggingCacheErrorHandler extends SimpleCacheErrorHandler {

        private final Logger logger = LoggerFactory.getLogger(this.getClass());

        @Override
        public void handleCacheGetError(RuntimeException exception, Cache cache, Object key) {
            System.err.println("\nRedisCacheGetError ---> " + exception.getMessage() + "\n");
//            logger.error(exception.toString()); // 不抛异常，记录日志
//            super.handleCacheGetError(exception, cache, key);
        }

        @Override
        public void handleCachePutError(RuntimeException exception, Cache cache, Object key, Object value) {
            System.err.println("\nRedisCachePutError ---> " + exception.getMessage() + "\n");
//            logger.error(exception.toString()); // 不抛异常，记录日志
//            super.handleCachePutError(exception, cache, key, value);
        }

        @Override
        public void handleCacheEvictError(RuntimeException exception, Cache cache, Object key) {
            System.err.println("\nRedisCacheEvictError ---> " + exception.getMessage() + "\n");
//            logger.error(exception.toString()); // 不抛异常，记录日志
//            super.handleCacheEvictError(exception, cache, key);
        }

        @Override
        public void handleCacheClearError(RuntimeException exception, Cache cache) {
            System.err.println("\nRedisCacheClearError ---> " + exception.getMessage() + "\n");
//            logger.error(exception.toString()); // 不抛异常，记录日志
//            super.handleCacheClearError(exception, cache);
        }

    }


}
